package com.uit.trip_.bridge;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.uit.trip_.models.CommentItem;
import com.uit.trip_.models.ImageDictionary;
import com.uit.trip_.models.Trip;

public class Parser {

	public static List<Trip> parseImageList(String response) {
		List<Trip> tripList = new ArrayList<Trip>();
		try {
			JSONArray jsonArray = new JSONArray(response);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jObject = jsonArray.getJSONObject(i);
				String imgId = jObject.getString("post_id");
				String id = jObject.getString("own_id");
				String name = jObject.getString("fb_name");
				String link = jObject.getString("post_img");
				String description = jObject.getString("post_content");
				String la = jObject.getString("x_latium");
				String lng = jObject.getString("y_longtium");
				String time = jObject.getString("date_add");
				int like = jObject.getInt("is_like");
				// Log.i("duongnx", "link:" + link);
				Trip trip = new Trip(id, imgId, name, description, link, la,
						lng, time, like);
				tripList.add(trip);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tripList;
	}

	public static List<CommentItem> parseListComments(String response) {

		List<CommentItem> commentList = new ArrayList<CommentItem>();
		try {
			JSONArray jsonArray = new JSONArray(response);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jObject = jsonArray.getJSONObject(i);
				String userId = jObject.getString("user_own");
				String userName = jObject.getString("fb_name");
				String comment = jObject.getString("content");
				String date = jObject.getString("date_add");
				CommentItem item = new CommentItem(userId, userName, comment,
						date);

				commentList.add(item);
			}
		} catch (JSONException e) {
			Log.e("duongnx", "commentexception:" + e);
		}
		return commentList;
	}

	public static List<String> parseListUserLikes(String response) {

		List<String> userList = new ArrayList<String>();
		try {
			JSONArray jArray = new JSONArray(response);
			for (int i = 0; i < jArray.length(); i++) {
				JSONObject jObject = jArray.getJSONObject(i);
				String user = jObject.getString("fb_name");
				userList.add(user);
			}
		} catch (JSONException e) {
			Log.e("duongnx", "like exception:" + e);
		}
		return userList;
	}

	public static ImageDictionary parseImageUploadInfo(String response) {
		ImageDictionary dic = new ImageDictionary();
		try {
			JSONObject jObject = new JSONObject(response);
			dic.imgId = jObject.getString("post_id");
			dic.link = jObject.getString("url_img");
			dic.date = jObject.getString("date_add");
		} catch (JSONException e) {
			Log.i("duongnx", "getImageUploadID exception");
		}
		return dic;
	}

	public static int parseResponseCode(String response) {
		int result = 0;
		try {
			JSONObject jObject = new JSONObject(response);
			result = jObject.getInt("responseCode");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}
